import java.util.Random;

public class Bank {
    public void transferMoney(Account from,Account to,double money){
        try{
            Random r = new Random();
            int rand = r.nextInt(10);
            System.out.println(Thread.currentThread().getName()+"开始转账++++++"+money+"元+++++++++++++++++++");
            Thread.currentThread().sleep(rand*1000);//加上了休息时间防止并发
            System.out.println(Thread.currentThread().getName()+"任务筹备了"+rand+"秒后准备完成， 出款账户余额："+from.getMoney()+"收款账户余额："+to.getMoney());
            synchronized (from){

                if (from.getMoney()-money<0){
                    System.out.println(Thread.currentThread().getName()+"任务 金额不足");
                    from.RecArr(Thread.currentThread().getName(),money,true);
                }else {
                    synchronized (to) {
                        from.setMoney(-money);
                        to.setMoney(money);
                        from.RecArr(Thread.currentThread().getName(),money,false);
                    }
                    System.out.println(Thread.currentThread().getName()+"任务"+from.getId() + "转账成功，" + to.getId() + "收到金额" + money + "元");
                }
            }
            System.out.println(Thread.currentThread().getName()+"转账结束++++++++"+money+"元+++++++++++++++++");
            System.out.println(Thread.currentThread().getName()+"任务 出款账户余额："+from.getMoney()+"收款账户余额："+to.getMoney());
            for(Object item:from.getDeductStr()){

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
